package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.pojo.Address;
import com.hmall.common.pojo.Item;
import com.hmall.common.pojo.User;
import com.hmall.common.search.ItemClient;
import com.hmall.common.search.OrderClient;
import com.hmall.common.search.UserClient;
import com.hmall.order.mapper.OrderDetailServiceMapper;
import com.hmall.order.mapper.OrderLogisticsMapper;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.pojo.RequestOrder;
import com.hmall.order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    UserClient userClient;

    @Autowired
    ItemClient itemClient;

    @Autowired
    OrderClient orderClient;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderDetailServiceMapper orderDetailServiceMapper;

    @Autowired
    OrderLogisticsMapper orderLogisticsMapper;


    @Override
    public Long orderBy(RequestOrder order) {

        //根据id查询商品
        Item item = itemClient.findItemById(Long.valueOf(order.getItemId()));
        //创建订单对象
        Order od = new Order();

        //将数据存储到数据库
        MakeOrderToSql(order, item,od);

        //将商品信息和orderId封装OrderDetail中并写入订单详情表
        OrderDetail orderDetail = new OrderDetail();

        MakeOrderDetailToSql(order, od, item, orderDetail);

        //调用地址查询功能   地址null
        Address address = userClient.findAddressById(Long.valueOf(order.getAddressId()));

        //需要一个根据id查询用户功能
        User user = userClient.findUserById(2);

        //将数据封装插入数据库
        MakeorderLogisticsToSql(address, user,od);

        //根据订单Id进行修改库存
        itemClient.updateItemNumById(orderDetail.getOrderId());

        System.out.println("返回的订单id"+od.getId());
        return od.getId();
    }

    public void MakeorderLogisticsToSql(Address address, User user,Order od) {
        OrderLogistics orderLogistics = new OrderLogistics();
        orderLogistics.setContact(user.getUsername());
        orderLogistics.setPhone(user.getPhone());
        //创建订单地址对象
        orderLogistics.setProvince(address.getProvince());
        orderLogistics.setCity(address.getCity());
        orderLogistics.setTown(address.getTown());
        orderLogistics.setStreet(address.getStreet());
        orderLogistics.setCreateTime(new Date());
        orderLogistics.setOrderId(od.getId());
        orderLogisticsMapper.insert(orderLogistics);
    }

    public void MakeOrderDetailToSql(RequestOrder order, Order od, Item item, OrderDetail orderDetail) {
        orderDetail.setOrderId(od.getId());
        orderDetail.setItemId(Long.valueOf(order.getItemId()));
        orderDetail.setNum(Integer.valueOf(order.getNum()));
        orderDetail.setTitle(item.getName());
        orderDetail.setPrice(item.getPrice());
        orderDetail.setSpec(item.getSpec());
        orderDetail.setImage(item.getImage());
        orderDetail.setCreateTime(new Date());
        //将订单详情添加到数据库
        orderDetailServiceMapper.insert(orderDetail);
    }

    public void MakeOrderToSql(RequestOrder order, Item item,Order od) {
        //获取商品总价
        long price = item.getPrice();
        long totalFee = price*Long.valueOf(order.getNum());
        //封装order对象
        //设置订单
        //设置商品金额
        od.setTotalFee(totalFee);
        //设置支付方式
        od.setPaymentType(Integer.valueOf(order.getPaymentType()));
        //初始化未支付
        od.setStatus(1);
        //设置创建订单时间
        od.setCreateTime(new Date());
        //设置用户id
        od.setUserId(2l);
        //将数据添加到数据库中
        orderMapper.insert(od);
    }
}
